<?php

/**
 * Форма правки макета
 */
class SnippetForm extends Zend_Form {

    /**
     * Форма макета
     *
     * @param int $sid #ID макета
     * @return SnippetForm
     */
    public function Snippet($sid = 0) {
        $this->setName('snippet');
        $t = Zend_Registry::get('Zend_Locale');
        $title = new Zend_Form_Element_Text('title');
        $title->setLabel('Title')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->setRequired();

        $file = new Zend_Form_Element_Text('file');
        $file->setLabel('File name')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->addValidator('NotEmpty')
                ->addValidator('Alnum')
                ->setRequired();


        $file->addValidator('Db_NoRecordExists', false, array(
            array('table' => 'snippets',
                'field' => 'file',
                'exclude' => array(
                    'field' => 'id',
                    'value' => $sid
                )
            )
        ));


        $code = new Zend_Form_Element_Textarea('editarea');
        $code->setLabel('Code')
                ->addValidator('NotEmpty');

        $model = new PageModel();
        $mid = new Zend_Form_Element_Select('mid');
        $array = $model->getAddonsList('page');
        $mid->addMultiOptions($array)
                ->setLabel('Module')
                ->addFilter('StripTags')
                ->addFilter('StringTrim')
                ->setRequired();

        $submit = new Zend_Form_Element_Submit('submit');
        $submit->setLabel('Save');
        $submit->setAttrib('id', 'submitbutton');

        $this->addElements(array($title, $file, $mid, $code, $submit));
        $this->setAction(ADMIN_URL . '/snippet/edit/id/' . $sid);
        $this->setDefaultTranslator($t);
        return $this;
    }

}